Cut, Copy, Paste

From Documentation



Overview

To copy a range of cells, you should use Ranges to select them and call paste(Range) with another Range object for destination like:

		Range src = Ranges.range(ss.getSelectedSheet(), ss.getSelection());
		Range destination = Ranges.range(getDestinationSheet(), ss.getSelection());
		src.paste(destination);

Similarly, the steps to cut a range is to copy them first, after pasting to destination, just clear the source range's content.

Cutting a range of cells is similar to copy:

		Range src = Ranges.range(ss.getSelectedSheet(), ss.getSelection());
		Range destination = Ranges.range(getDestinationSheet(), ss.getSelection());
		src.paste(destination, true);

There is also a pasteSpecial() to do special pasting like pasting value only or pasting formula only.

With the help of CellOperationUtil, we can easily perform copying and cutting, and it also provides methods for "paste special" such as pasteValue(), or pasteFormula(). These methods all require 2 Range objects as arguments. One is source and another is destination.

To cut a range of cells, you should use

CellOperationUtil.cut(srcRange, destRange);

To copy a range of cells, you should use

CellOperationUtil.paste(srcRange, destRange);

The usages for pasteFormula(), pasteValue(), pasteTranspose(), and pasteAllExceptBorder() are all the same.

Example

The following codes copy a selection range to the same position of another sheet when a user clicks a button.

public class CopyCutComposer extends SelectorComposer<Component> {

	@Wire
	private Spreadsheet ss;


	@Listen("onClick = #copyButton")
	public void copyByUtil() {
		Range src = Ranges.range(ss.getSelectedSheet(), ss.getSelection());
		Range dest = Ranges.range(getResultSheet(), ss.getSelection());
		CellOperationUtil.paste(src, dest);
	}
	
	//omitted codes...
}



All source code listed in this book is at Github.


Last Update : 2022/01/19

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.